package com.ruoyi.system.commons.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther Yulong Liu
 * @Date 2023/12/27 17:50
 * @Description
 **/


@Slf4j
public class OneMapVisionUtils {

    public static String decorationViewOneMapRiverQuery(String riverJson){
        /**@Description: 将接收到的参数翻译成前端想要的结果
         * @param [riverJson]
         * @return java.lang.String
         */
//        String riverJson = "{ \"河流编码\": \"123\", \"河流名称\": \"长江\" }";
        String filterJson = "{\n" +
                "    \"elementType\": \"river\",\n" +
                "    \"filterOptions\": [\n" +
                "        {\n" +
                "            \"name\": \"河流\",\n" +
                "            \"elementType\": \"river\",\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                     \"filterKey\": \"河流编码\"," +
                "                     \"filterValue\": \"obj_code\"," +
                "                     \"value\": null," +
                "                     \"valueMin\": null," +
                "                     \"valueMax\": null," +
                "                     \"dataType\": \"string\"," +
                "                     \"displayType\": \"input\"," +
                "                     \"displayOptions\": []" +
                "                 }," +
                "                 {" +
                "                    \"filterKey\": \"河流名称\"," +
                "                    \"filterValue\": \"obj_name\"," +
                "                    \"value\": null," +
                "                    \"valueMin\": null," +
                "                    \"valueMax\": null," +
                "                    \"dataType\": \"string\"," +
                "                    \"displayType\": \"input\"," +
                "                    \"displayOptions\": []" +
                "                 }" +
                "            ]" +
                "        }" +
                "    ]" +
                "}";
        log.info("接收到的的json:{}",riverJson);
        JSONObject riverObject = JSON.parseObject(riverJson);
        JSONObject filterFinal = JSON.parseObject(filterJson);
        JSONArray filterArray = filterFinal.getJSONArray("filterOptions").getJSONObject(0).getJSONArray("filters");
//        log.info("filterArray{}",filterFinal.toString());
//        log.info("拆解了什么东西{}",filterArray.toString());
//        JSONArray filterArray = JSON.parseArray(filterJson);

        for (int i = 0; i < filterArray.size(); i++) {
            JSONObject filterObject = filterArray.getJSONObject(i);
            String filterKey = filterObject.getString("filterKey");

            if (riverObject.containsKey(filterKey)) {
                String value = riverObject.getString(filterKey);
                filterObject.put("value", value);
            }
        }
        filterFinal.getJSONArray("filterOptions").getJSONObject(0).put("filters", filterArray);
//        System.out.println(filterFinal.toJSONString());
        log.info("发送出去的json:{}",filterFinal.toJSONString());
        return filterFinal.toJSONString();
    }


    public static String decorationViewOneMapRswbQuery(String queryJson){
//        String queryJson = "{\"水库编码\":\"ahahahah\", \"水库名称\":\"hahahah\"}";
        String filterJson = "{\n" +
                "    \"elementType\": \"rswb_dam\",\n" +
                "    \"filterOptions\": [\n" +
                "        {\n" +
                "            \"parentElementType\": \"rswb_dam\",\n" +
                "            \"elementType\": \"rswb\",\n" +
                "            \"name\": \"水库\",\n" +
                "            \"show\": true,\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                    \"filterKey\": \"水库编码\",\n" +
                "                    \"filterValue\": \"res_code\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"水库名称\",\n" +
                "                    \"filterValue\": \"res_name\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"工程规模\",\n" +
                "                    \"filterValue\": \"eng_scal\",\n" +
                "                    \"value\": [],\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"array\",\n" +
                "                    \"displayType\": \"checkbox\",\n" +
                "                    \"displayOptions\": [\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（1）型\",\n" +
                "                            \"optionValue\": \"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（2）型\",\n" +
                "                            \"optionValue\": \"2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"中型\",\n" +
                "                            \"optionValue\": \"3\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（1）型\",\n" +
                "                            \"optionValue\": \"4\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（2）型\",\n" +
                "                            \"optionValue\": \"5\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"未知\",\n" +
                "                            \"optionValue\": \"0\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"parentElementType\": \"rswb_dam\",\n" +
                "            \"elementType\": \"dam\",\n" +
                "            \"name\": \"大坝\",\n" +
                "            \"show\": true,\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                    \"filterKey\": \"大坝编码\",\n" +
                "                    \"filterValue\": \"dam_code\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"大坝名称\",\n" +
                "                    \"filterValue\": \"dam_name\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"工程规模\",\n" +
                "                    \"filterValue\": \"eng_scal\",\n" +
                "                    \"value\": [],\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"array\",\n" +
                "                    \"displayType\": \"checkbox\",\n" +
                "                    \"displayOptions\": [\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（1）型\",\n" +
                "                            \"optionValue\": \"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（2）型\",\n" +
                "                            \"optionValue\": \"2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"中型\",\n" +
                "                            \"optionValue\": \"3\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（1）型\",\n" +
                "                            \"optionValue\": \"4\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（2）型\",\n" +
                "                            \"optionValue\": \"5\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"未知\",\n" +
                "                            \"optionValue\": \"0\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        JSONObject rswbDamJson= JSON.parseObject(queryJson);
        JSONObject filterFinal = JSON.parseObject(filterJson);
        JSONArray filterRswbArray = filterFinal.getJSONArray("filterOptions").getJSONObject(0).getJSONArray("filters");
        for (int i = 0; i < filterRswbArray.size(); i++) {
            JSONObject filterObject = filterRswbArray.getJSONObject(i);
            String filterKey = filterObject.getString("filterKey");

            if (rswbDamJson.containsKey(filterKey)) {
                String targetValue = rswbDamJson.getString(filterKey);
                filterObject.put("value", targetValue);
            }
        }
        filterFinal.getJSONArray("filterOptions").getJSONObject(0).put("filters", filterRswbArray);

        JSONObject newJson = new JSONObject();
        newJson.put("大坝编码", rswbDamJson.get("水库编码"));
        newJson.put("大坝名称", rswbDamJson.get("水库名称"));

        JSONArray filterDamArray = filterFinal.getJSONArray("filterOptions").getJSONObject(1).getJSONArray("filters");
        for (int i = 0; i < filterDamArray.size(); i++) {
            JSONObject filterObject = filterDamArray.getJSONObject(i);
            String filterKey = filterObject.getString("filterKey");
            if (newJson.containsKey(filterKey)) {
                String value = newJson.getString(filterKey);
                filterObject.put("value", value);
            }
        }
        filterFinal.getJSONArray("filterOptions").getJSONObject(1).put("filters", filterRswbArray);
        log.info("水库filterFinal:{}", filterFinal.toString());

        return filterFinal.toString();
    }


    private static JSONObject scaleTool(String scale){
        JSONObject jsonObject = new JSONObject();
        // 往 JSONObject 对象中添加键值对
        jsonObject.put("optionValue", scale);

        return jsonObject;
    }


    //TODO:存档 考虑了
    public static String decorationViewOneMapRswbQueryTMPTMPTMP(String queryJson){
//        String queryJson = "{\"水库编码\":\"ahahahah\", \"水库名称\":\"hahahah\",\"工程规模\":\"大一型,大二型\"}";
        String filterJson = "{\n" +
                "    \"elementType\": \"rswb_dam\",\n" +
                "    \"filterOptions\": [\n" +
                "        {\n" +
                "            \"parentElementType\": \"rswb_dam\",\n" +
                "            \"elementType\": \"rswb\",\n" +
                "            \"name\": \"水库\",\n" +
                "            \"show\": true,\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                    \"filterKey\": \"水库编码\",\n" +
                "                    \"filterValue\": \"res_code\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"水库名称\",\n" +
                "                    \"filterValue\": \"res_name\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"工程规模\",\n" +
                "                    \"filterValue\": \"eng_scal\",\n" +
                "                    \"value\": [],\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"array\",\n" +
                "                    \"displayType\": \"checkbox\",\n" +
                "                    \"displayOptions\": [\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（1）型\",\n" +
                "                            \"optionValue\": \"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（2）型\",\n" +
                "                            \"optionValue\": \"2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"中型\",\n" +
                "                            \"optionValue\": \"3\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（1）型\",\n" +
                "                            \"optionValue\": \"4\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（2）型\",\n" +
                "                            \"optionValue\": \"5\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"未知\",\n" +
                "                            \"optionValue\": \"0\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"parentElementType\": \"rswb_dam\",\n" +
                "            \"elementType\": \"dam\",\n" +
                "            \"name\": \"大坝\",\n" +
                "            \"show\": true,\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                    \"filterKey\": \"大坝编码\",\n" +
                "                    \"filterValue\": \"dam_code\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"大坝名称\",\n" +
                "                    \"filterValue\": \"dam_name\",\n" +
                "                    \"value\": null,\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"string\",\n" +
                "                    \"displayType\": \"input\",\n" +
                "                    \"displayOptions\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"filterKey\": \"工程规模\",\n" +
                "                    \"filterValue\": \"eng_scal\",\n" +
                "                    \"value\": [],\n" +
                "                    \"valueMin\": null,\n" +
                "                    \"valueMax\": null,\n" +
                "                    \"dataType\": \"array\",\n" +
                "                    \"displayType\": \"checkbox\",\n" +
                "                    \"displayOptions\": [\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（1）型\",\n" +
                "                            \"optionValue\": \"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"大（2）型\",\n" +
                "                            \"optionValue\": \"2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"中型\",\n" +
                "                            \"optionValue\": \"3\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（1）型\",\n" +
                "                            \"optionValue\": \"4\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"小（2）型\",\n" +
                "                            \"optionValue\": \"5\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"optionName\": \"未知\",\n" +
                "                            \"optionValue\": \"0\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        JSONObject rswbDamJson= JSON.parseObject(queryJson);
        JSONObject filterFinal = JSON.parseObject(filterJson);

        String tmp = rswbDamJson.get("工程规模").toString();

        JSONArray scaleArray = new JSONArray();
        if (tmp.contains("大一")) {scaleArray.add(scaleTool("1"));}
        if (tmp.contains("大二")){scaleArray.add(scaleTool("2"));}
        if (tmp.contains("中")){scaleArray.add(scaleTool("3"));}
        if (tmp.contains("小一")){scaleArray.add(scaleTool("4"));}
        if (tmp.contains("小二")){scaleArray.add(scaleTool("5"));}
        if (tmp.contains("未知")){scaleArray.add(scaleTool("0"));}
        rswbDamJson.put("工程规模",scaleArray);



        JSONArray filterRswbArray = filterFinal.getJSONArray("filterOptions").getJSONObject(0).getJSONArray("filters");
        for (int i = 0; i < filterRswbArray.size(); i++) {
            JSONObject filterObject = filterRswbArray.getJSONObject(i);
            String filterKey = filterObject.getString("filterKey");

            if (rswbDamJson.containsKey(filterKey)) {
                Object targetValue = rswbDamJson.get(filterKey);
                filterObject.put("value", targetValue);
            }
        }
        filterFinal.getJSONArray("filterOptions").getJSONObject(0).put("filters", filterRswbArray);

        JSONObject newJson = new JSONObject();
        newJson.put("大坝编码", rswbDamJson.get("水库编码"));
        newJson.put("大坝名称", rswbDamJson.get("水库名称"));
        newJson.put("工程规模", rswbDamJson.get("工程规模"));

        JSONArray filterDamArray = filterFinal.getJSONArray("filterOptions").getJSONObject(1).getJSONArray("filters");
        for (int i = 0; i < filterDamArray.size(); i++) {
            JSONObject filterObject = filterDamArray.getJSONObject(i);
            String filterKey = filterObject.getString("filterKey");
            if (newJson.containsKey(filterKey)) {
                String value = newJson.getString(filterKey);
                filterObject.put("value", value);
            }
        }
        filterFinal.getJSONArray("filterOptions").getJSONObject(1).put("filters", filterRswbArray);
        log.info("水库filterFinal:{}", filterFinal.toString());

        return filterFinal.toString();
    }


}
